Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support package exports #6125

Open
wants to merge 2 commits into
base: v-next
Choose a base branch
from
Open

Support package exports #6125

wants to merge 2 commits into from

Conversation

antico5
Copy link
Contributor

@antico5 antico5 commented Jan 8, 2025

This PR adds support for package.json#exports for packages that distribute solidity files.

Implementation details:

  • Source names for npm modules use the subpath without resolving, e.g. npm/[email protected]/Foo.sol instead of npm/[email protected]/src/Foo.sol, for an import of the kind import 'mypkg/Foo.sol'. This is arbitrary
  • Module resolving was changed from using require.resolve to manual lookup using the help of require.resolve.paths, to support packages that don't export their package.json
  • Exports resolving make use of the resolve.exports package. In this case we avoid using require.resolve because it may resolve to different packages in the filesystem for a given package name and version.

Known issues:

  • When trying to import a module with wrong casing, in a module that uses exports, I'm not sure how we can suggest the correct casing. E.g. mypkg/TheContract.sol resolves to mypkg/src/TheContract.sol. If user does import 'mypkg/thecontract.sol', I know that the correct resolved path is src/TheContract.sol, but I'm not sure how to it back to TheContract.sol. Asking for suggestions on how to approach this

Closes #5992

@antico5 antico5 added the v-next A Hardhat v3 development task label Jan 8, 2025
@antico5 antico5 requested a review from alcuadrado January 8, 2025 17:36
Copy link

vercel bot commented Jan 8, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hardhat ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 9, 2025 4:34pm

Copy link

changeset-bot bot commented Jan 8, 2025

⚠️ No Changeset found

Latest commit: 58f0de7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

github-actions bot commented Jan 8, 2025

hardhat

Total size of the bundle: 219M
Total number of dependencies (including transitive): 54

List of dependencies (sorted by size)
214M	total
30M	@ignored/edr-optimism-linux-x64-musl
30M	@ignored/edr-optimism-linux-x64-gnu
27M	@ignored/edr-optimism-linux-arm64-musl
27M	@ignored/edr-optimism-linux-arm64-gnu
22M	@ignored/edr-optimism-win32-x64-msvc
21M	@ignored/edr-optimism-darwin-x64
20M	esbuild
20M	@ignored/edr-optimism-darwin-arm64
2.8M	@sentry/tracing
2.5M	micro-eth-signer
1.9M	@noble/curves
1.7M	undici
1.2M	@sentry/types
1.2M	@noble/hashes
932K	@sentry/node
920K	@sentry/utils
856K	zod
848K	@ignored/hardhat-vnext-utils
624K	micro-packed
576K	tsx
548K	@sentry/core
544K	fast-equals
492K	@scure/bip39
460K	@ignored/edr
384K	@ignored/edr-optimism
368K	ethereum-cryptography
344K	@sentry/hub
324K	@ignored/hardhat-vnext-errors
320K	enquirer
284K	semver
192K	ws
168K	@scure/base
136K	adm-zip
128K	get-tsconfig
96K	@scure/bip32
92K	chalk
88K	tslib
88K	@sentry/minimal
76K	agent-base
72K	@nomicfoundation/solidity-analyzer
68K	debug
64K	lru_map
64K	https-proxy-agent
60K	@ignored/hardhat-vnext-zod-utils
56K	rfdc
48K	ansi-colors
44K	resolve.exports
40K	resolve-pkg-maps
36K	p-map
32K	cookie
24K	strip-ansi
24K	env-paths
24K	ansi-regex
20K	ms

Comment on lines +128 to +129
await fs.promises.access(packageJsonPath, fs.constants.R_OK);
return await fs.promises.realpath(packageJsonPath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use the utils' fs module

* @param packageName the name of the package to find
* @returns the absolute real path (resolved symlinks) of the package.json
*/
export async function findPackageJson(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should at at least one or two tests of this

@@ -1,3 +1,5 @@
import type { Exports } from "resolve.exports";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should duplicate this type to avoid re-exporting a dependency type

@alcuadrado
Copy link
Member

Note to self:

Double check that this situation:

import "foo/a.sol";

where foo has

exports: {
  "./*.sol": "./src/*.sol"
}

and ./src/a.sol doesn't exist.

throws that "foo/a.sol" doesn't exist, and not "foo/src/a.sol".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:triaging v-next A Hardhat v3 development task
Projects
Status: Backlog
Development

Successfully merging this pull request may close these issues.

3 participants